package com.ftd.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ftd.DBConnection.DBConnection;
import com.ftd.model.Ebook;
import com.ftd.viewmodel.ApiResult;
import com.ftd.viewmodel.MapResult;
import org.apache.http.Consts;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.fluent.Content;
import org.apache.http.client.fluent.Request;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by apple on 16/8/2.
 */
@RestController
public class EbookViewDetailController {
    @RequestMapping(value = "getEbookDetailJson")
    public ApiResult getEbookDetailJson(String index)  {
        ArrayList list = (ArrayList) DBConnection.select1("ebookTable");
        MapResult mapResult = new MapResult();

        for (int i = 0; i < list.size();i++) {
            Ebook ebook = (Ebook)list.get(i);
            if (ebook.getTag().equals(index)){
                mapResult.put("bookName", ebook.geteBookName());
                mapResult.put("imageUrl", ebook.geteImageUrl());
                mapResult.put("review", ebook.geteBookReview());
                mapResult.put("reading", ebook.geteBookReading());
                mapResult.put("author",ebook.getAuthor());
                mapResult.put("bookUrl",ebook.getBookUrl());
                break;
            }
            System.out.print(ebook.geteBookName());
        }

        return mapResult;
    }
    @RequestMapping("addCollection")
    public boolean addCollection(String phonenumber , String bookTag,String n){
        DBConnection.update(phonenumber,bookTag);

        return true;
    }
    //方法,判断数据库tag这一列是否是1,如果是1,则还未收藏,就会显示灰色
    @RequestMapping("isCollection")
    public boolean isCollection(String phonenumber,String tag){
        ArrayList list = DBConnection.select6("collectionTable",phonenumber);
        System.out.print(list.size());
        for (int i = 0; i < list.size(); i++) {
            String tag1 = list.get(i).toString();
            System.out.print(tag1);
            System.out.print(tag);
            if (tag1.equals(tag)){
                return true;
            }
        }

        return false;
    }

    @RequestMapping("getCollection")
    public ApiResult getCollection(String phonenumber){
        ArrayList list = DBConnection.select6("collectionTable",phonenumber);
        ArrayList list2 = (ArrayList) DBConnection.select1("ebookTable");
        ArrayList<ApiResult> list3 = new ArrayList<ApiResult>();
        MapResult mapResult = new MapResult();
        ArrayList<Ebook> bookList = new ArrayList<Ebook>();
        for(int i =0 ; i<list.size();i++) {
            String tag = list.get(i).toString();
            if (!tag.isEmpty()) {
                System.out.println(tag);
                int iTag = Integer.parseInt(tag);
                System.out.println(iTag);
                Ebook ebook = (Ebook) list2.get(iTag);
                bookList.add(ebook);
            }
        }
        mapResult.put("bookList",bookList);

        return mapResult;
    }

}
